package com.ft.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.ft.domain.Buyorder;
import com.ft.domain.Sendorder;
import com.ft.service.IBuyorderService;
import com.ft.service.ISendorderService;
import com.ft.utils.Code;
import com.ft.utils.Result;
import com.ft.utils.SnowFlakeGenerateIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author com/ft
 * @since 2022-08-24
 */
@RestController
@RequestMapping("/sendorder")
public class SendorderController {
    @Autowired
    private ISendorderService sendorderService;

    /**
     * 创建订单
     *
     * @param sendorder 订单对象
     * @return 创建的订单对象
     * @throws JsonProcessingException
     */
    @PostMapping()
    public Result creatrBuyOrder(@RequestBody Sendorder sendorder) throws JsonProcessingException {
        //创建订单编号
        SnowFlakeGenerateIdWorker snowFlakeGenerateIdWorker =
                new SnowFlakeGenerateIdWorker (0L, 0L);
        String id = snowFlakeGenerateIdWorker.generateNextId ();
        //设置订单编号
        sendorder.setId (Long.valueOf (id));
        //返回创建结果
        int result = sendorderService.createSendorder(sendorder);
        if (result == 1) {
            Sendorder sendorder1 = sendorderService.selectBuyOrder(Long.valueOf (id));
            return new Result (Code.POST_OK, sendorder1, "创建订单成功");
        } else {
            return new Result (Code.POST_ERR, "创建订单失败");
        }
    }
}

